package com.github.niefy.modules.third.controller;

import com.github.niefy.common.utils.R;
import com.github.niefy.modules.third.entity.ThirdTemplateMsgDetail;
import com.github.niefy.modules.third.enums.TemplateMsgPushFlagEnum;
import com.github.niefy.modules.third.service.ThirdTemplateMsgDetailService;
import com.github.niefy.modules.third.service.ThirdTemplateMsgUnreadService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.mp.api.WxMpService;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 二三方模板消息操作
 */
@Controller
@RequestMapping("/third/templateMsg")
@Tag(name = "二三方模板消息操作")
@RequiredArgsConstructor
@Slf4j
public class ThirdTemplateMsgController {

    private final WxMpService wxMpService;
    private final ThirdTemplateMsgUnreadService thirdTemplateMsgUnreadService;
    private final ThirdTemplateMsgDetailService thirdTemplateMsgDetailService;

    /*
    * 查询未读消息数量
    * */
    @GetMapping("/getUnreadMsgCount")
    @ResponseBody
    @Operation(summary = "获取未读消息数量",description = "根据thirdId获取未读消息数量")
    public R sendOnceMsg(HttpServletRequest request, HttpServletResponse response,
                         @RequestAttribute String thirdId) throws WxErrorException {
        //var thirdId = (String)request.getAttribute("thirdId");
        Long count = thirdTemplateMsgUnreadService.getUnreadMsgCount(thirdId);
        return R.ok().put("unreadMsgCount", count);
    }

    /**
     * 根据msgId及thirdId返回html响应，内容为msg_content
     * @param request
     * @param response
     * @return
     * @throws WxErrorException
     */
    @GetMapping(value = "/getMsgContent/{msgId}")
    @Transactional
    @ResponseBody
    @Operation(summary = "查询消息内容",description = "根据msgId及thirdId返回html响应，内容为msg_content")
    public String getMsgContent(HttpServletRequest request, HttpServletResponse response,
                                @PathVariable Long msgId,
                                @RequestAttribute String thirdId) throws WxErrorException {
        var msgContent = thirdTemplateMsgDetailService.getMsgContent(msgId, thirdId);
        msgContent = "<!DOCTYPE html>\n" +
                "<html lang=\"en\">\n" +
                "<head>\n" +
                "<meta name=\"viewport\" content=\"width=device-width,initial-scale=1,user-scalable=0\">\n" +
                "  <meta charset=\"UTF-8\">\n" +
                "  </head>"+msgContent+"</html>";
        response.setContentType("text/html;charset=UTF-8");
        //response.setHeader("Content-Type","document");
        return msgContent;
    }

    @GetMapping("/getMsgBaseInfo")
    @ResponseBody
    @Operation(summary = "获取未读消息基本信息",description = "获取未读消息基本信息，包含msgTitle，operateDate等")
    public R getMsgBaseInfo(HttpServletRequest request, HttpServletResponse response,
                                @RequestAttribute String thirdId) throws WxErrorException {
        var thirdTemplateMsgDetails = thirdTemplateMsgDetailService.lambdaQuery()
                .eq(ThirdTemplateMsgDetail::getStudentNo, thirdId)
                .eq(ThirdTemplateMsgDetail::getPushFlag, TemplateMsgPushFlagEnum.TWO.getValue())
                .select(ThirdTemplateMsgDetail::getMsgId,
                        ThirdTemplateMsgDetail::getMsgTitle,
                        ThirdTemplateMsgDetail::getOperateDate,
                        ThirdTemplateMsgDetail::getMsgTypeName)
                .list();
        return R.ok().put("msgBaseInfos", thirdTemplateMsgDetails);
    }


}
